package com.dengqi.mymodule.activity.network.module;

import com.dengqi.mymodule.activity.network.base.BasePresenter;
import com.dengqi.mymodule.activity.network.bean.AccountBean;
import com.dengqi.mymodule.activity.network.bean.AccountBusinessBean;
import com.dengqi.mymodule.activity.network.util.CheckUtil;
import com.dengqi.mymodule.activity.network.util.ConstantUtil;
import com.module.retrofit.bean.ResponseBean;
import com.module.retrofit.RetrofitHelper;
import com.module.retrofit.callback.BaseObserver;
import com.module.retrofit.utils.HeaderUtils;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;


/**
 * @Author: dengqi
 * @CreateDate: 2020-10-15 19:51
 * @Description:
 */
public class AccountPresenter extends BasePresenter implements AccountContract.Presenter {


    private AccountContract.Model model;

    public AccountPresenter(RxAppCompatActivity activity){
        super(activity);
        this.model = RetrofitHelper.getApiService(AccountContract.Model.class);
    }


    @Override
    public void login(AccountContract.LoginView view) {
        String account = view.getAccount();
        String password = view.getPassWord();
        String captcha = view.getCaptcha();
        //1=短信验证码登录 2=密码登录
        String loginType = view.getLoginType();
        if(!CheckUtil.checkPhone(account) ||
                !CheckUtil.checkPassword(password) && "2".equals(loginType) ||
                !CheckUtil.checkCaptcha(captcha) && "1".equals(loginType)){
            return;
        }
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("phone", account);
        parmas.put("loginType", loginType);
        parmas.put("captcha", captcha);
        parmas.put("password", password);
        execute(model.login(parmas))
                .subscribe(new BaseObserver<ResponseBean<AccountBean>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<AccountBean> result) {
                        saveUserInfo(result.getData());
                        saveHeaders(result.getData());
                        view.onLogin(result.getData());

                    }

                });
    }

    @Override
    public void getAccountType(AccountContract.AccountTypeView view) {
        execute(model.getAccountType())
                .subscribe(new BaseObserver<ResponseBean<AccountBusinessBean>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<AccountBusinessBean> result) {
                        view.onGetAccountType(result.getData());

                    }

                });
    }

    @Override
    public void login2(AccountContract.LoginView loginView, AccountContract.AccountTypeView accountTypeView) {
        String account = loginView.getAccount();
        String password = loginView.getPassWord();
        String captcha = loginView.getCaptcha();
        //1=短信验证码登录 2=密码登录
        String loginType = loginView.getLoginType();
        if(!CheckUtil.checkPhone(account) ||
                !CheckUtil.checkPassword(password) && "2".equals(loginType) ||
                !CheckUtil.checkCaptcha(captcha) && "1".equals(loginType)){
            return;
        }
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("phone", account);
        parmas.put("loginType", loginType);
        parmas.put("captcha", captcha);
        parmas.put("password", password);
        execute(model.login(parmas))
                .flatMap(new Function<ResponseBean<AccountBean>, ObservableSource<ResponseBean<AccountBusinessBean>>>() {
                    @Override
                    public ObservableSource<ResponseBean<AccountBusinessBean>> apply(ResponseBean<AccountBean> result) throws Exception {
                        saveUserInfo(result.getData());
                        saveHeaders(result.getData());
                        return execute(model.getAccountType());
                    }
                })
                .subscribe(new BaseObserver<ResponseBean<AccountBusinessBean>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<AccountBusinessBean> result) {
                        accountTypeView.onGetAccountType(result.getData());

                    }

                });
    }

    @Override
    public void editPassword(AccountContract.EditPasswordView view) {
        String phone = view.getPhone();
        String password = view.getPassWord();
        String rePassword = view.getRePassWord();
        String captcha = view.getCaptcha();
        if(!CheckUtil.checkPhone(phone) ||
                !CheckUtil.checkPassword(password) ||
                !CheckUtil.checkCaptcha(captcha) ||
                !CheckUtil.checkRePassword(password, rePassword)){
            return;
        }
        Map<String, String> parmas = new LinkedHashMap<>();
        parmas.put("phone", phone);
        parmas.put("captcha", captcha);
        parmas.put("password", password);
        execute(model.editPassword(parmas))
                .subscribe(new BaseObserver<ResponseBean<Map<String, String>>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<Map<String, String>> result) {
                        view.onEditPassword();
                    }

                });
    }

    @Override
    public void logout(AccountContract.LogoutView view) {
        execute(model.logout())
                .subscribe(new BaseObserver<ResponseBean<Map<String, String>>>(activity) {
                    @Override
                    public void onSuccess(ResponseBean<Map<String, String>> result) {
                        clearHeaders();
                        view.onLogout();
                    }
                });
    }

    /**
     * 保存userBean
     */
    private void saveUserInfo(AccountBean object) {

    }


    /**
     * 保存token
     * @param object
     */
    private void saveHeaders(AccountBean object) {
        HeaderUtils.setHeader(ConstantUtil.HEADER_TOKEN, object.getToken());
    }

    /**
     * 清除token
     */
    private void clearHeaders() {
        HeaderUtils.clearHeaders(ConstantUtil.HEADER_TOKEN);
    }

}
